<?php
namespace Application\pform\a_wan;


use Cola\Model;
use Cola\Request;
use \Application\model\PlayerModel as basePlayerModel;
class PlayerModel extends basePlayerModel
{
    protected $v = 1;

    /**
     * 获取玩家区服信息
     * @return array
     */
    public function getRoleInfo()
    {
        $rows = $this->requestAll();
        $sign = $rows['sign'];
        $sid = $rows['srv_id'];
        $actorId = $rows['role_id'];
        unset($rows['sign']);
        if (!$this->sign($rows, $sign)) return $this->result(0, '验签失败');

        $server = $this->model('Server')->getServerBySid($sid);
        $content = [];
        if ($server) {
            $openTime = strtotime(date('Y-m-d', strtotime($server['open_time'])));
            $time = strtotime(date('Y-m-d'));
            $diff = floor(abs($openTime - $time) / 86400)+1;

            // 获取合服
            $content = [
                'player_id' => $actorId,
                'srv_id' => $sid,
                'srv_name' => $server['name'],
                'merge_count' => 0,
                'merge_days' => 0,
                'open_days' => $diff,
            ];
        }

        return $this->result(1, '操作成功', $content);

    }

    /**
     * 获取区服列表
     * @return array
     */
    public function getServers()
    {
        $rows = $this->requestAll();
        $sign = $rows['sign'];
        $cpid = $rows['xx_game_id'];
        if (!$this->sign($rows, $sign)) {
            return $this->result(0, 'sign error');
        }
        return $this->result(1, 'success', $this->serverList([
            'cpid' => $cpid,
            'page' => $rows['page'] ?? 1,
        ]));

    }

    /**
     * 封禁玩家
     * @return array
     */
    public function banPlayer($isUnban=false)
    {
        $arr = $this->ban();
        if (empty($arr)) return $this->result(0, '验签失败');
        $data = $this->model('Ban')->handle($arr,$isUnban);
        if ($data) {
            return $this->result(1, '操作成功',$data);
        }
        return $this->result(0, '操作失败');

    }

    /**
     * 解封玩家
     * @return array
     */
    public function unbanPlayer()
    {
        return $this->banPlayer(true);
    }

    /**
     * 禁言
     * @return array
     */
    public function banChat($isUnban=false)
    {
        $arr = $this->ban();
        if (empty($arr)) return $this->result(0, '验签失败');
        $arr['type'] = BAN_CHAT;
        if($arr['inductive']){
            $arr['type'] = BAN_NONLIDUCTIVE_BLACK;
        }
        $data = $this->model('Ban')->handle($arr,$isUnban);
        if ($data) {
            return $this->result(1, '操作成功',$data);
        }
        return $this->result(0, '操作失败');

    }

    /**
     * 解禁言
     * @return array
     */
    public function unbanChat()
    {
        return $this->banChat(true);
    }

    public function kickOut()
    {
        $arr = $this->ban();
        if (empty($arr)) return $this->result(0, '验签失败');
        $arr['type'] = BAN_KICKOUT;
        $data = $this->model('Ban')->handle($arr);
        if ($data) {
            return $this->result(1, '操作成功',$data);
        }
        return $this->result(0, '操作失败');

    }

    public function notifyServer()
    {
        $rows = [];
        $rows['channel'] = CHANNEL_ID;
        $rows['xx_game_id'] = $this->get('game_id');
        $rows['server_name'] = $this->get('name');
        $rows['server_num'] = $this->get('sid');
        $rows['server_created_at'] = $this->get('open_time');
        $rows['type'] = $this->get('type'); // 1:自动开服 2:手动开服
        ksort($rows);

        $salt = SERVER_KEY;
        $rows['sign']=  strtolower(md5(http_build_query($rows) . $salt));
        $res = $this->curlWay(SERVER_NOTIFY_URL,$rows);

        $this->logfile('notify server:'.json_encode($rows).' result:'.json_encode($res));
        return $this->result(1, '', $res);
    }



    public function syncChat()
    {

        $account = $this->get('account');
        $account = substr($account, strpos($account, '-') + 1);
        $targetAccount = $this->get('target_account');
        $targetAccount = substr($targetAccount, strpos($targetAccount, '-') + 1);
        $data = $target = $rows = [];
        $channel = $this->get('channel');
        // 世界频道不上报
        $typeMap = [ 2=>2,3=>3,4=>1 ];
        if ($channel === 1 || !isset($typeMap[$channel])) return $this->result(1, '');

        $data['channel_id'] = $this->get('ccid');
        $data['xx_game_id'] = GAME_ID;
        $data['server_id'] = $this->get('sid');
        $data['channel_user_id'] = $account;
        $data['role_id'] = $this->get('actor_id');
        $data['role_name'] = $this->get('actor_name');
        $data['role_level'] = $this->get('level');
        $data['user_ip'] = $this->get('ip');
        $data['vip_level'] = $this->get('vip');
        $data['chat_type'] = $typeMap[$channel];
        $data['contents'] = $this->get('content');
        $data['chat_at'] = strtotime($this->get('chat_dt'));
        $data['timestamp'] = time();
        ksort($data);
        $data['money'] = $this->get('amount');
        $target['role_id'] = $this->get('target');
        $target['server_id'] = $this->get('target_sid');
        $target['channel_user_id'] = $targetAccount;
        $target['role_name'] = $this->get('target_actor_name');
        $target['role_level'] = $this->get('target_level');
        $target['vip_level'] = $this->get('target_vip');
        if ($data['chat_type'] === 1) $data['chat'] = $target;
        $sign_field = [
            'xx_game_id',
            'channel_id',
            'server_id',
            'channel_user_id',
            'role_id',
            'role_name',
            'role_level',
            'user_ip',
            'vip_level',
            'chat_type',
            'contents',
            'chat_at',
            'timestamp'
        ];
        $salt = $this->v==2 ? CHAT_KEY : GAME_SECRET;
        list($before,$data['sign']) = PublicModel::signV21($data,$salt,array_keys(array_diff_key($data,array_flip($sign_field))));
        $res = $this->curlWay(SYNC_CHAT_URL,$data);
        $this->logfile(SYNC_CHAT_URL.':v=='.$this->v);
        $this->logfile('sync chat:'.json_encode($data).' result:'.json_encode($res,JSON_UNESCAPED_UNICODE));
        return $this->result(1,'',$res);
    }



    protected function ban()
    {
        $rows = $this->requestAll();
        $sign = $rows['sign'];unset($rows['sign']);
        $array = $this->banParam($rows);

        if($this->v==2){
            if(!$this->sign($rows, $sign,CHAT_KEY)) return [];
            return $array;
        }
        if (!$this->sign($rows, $sign)) return [];

        return $array;
    }

    protected function banParam($rows)
    {
        $arr['sid'] = $rows['server'] ?? '';
        $arr['actor_id'] = $rows['role_id'] ?? '';
        $arr['account'] = $rows['user'] ?? '';
        $arr['role_name'] = $rows['role_name'] ?? '';
        $arr['clearChat'] = $rows['clearChat'] ?? 1;//默认清除
        $arr['inductive'] = $rows['inductive'] ?? 0;
        if (isset($rows['duration'])) $arr['bantime'] = intval($rows['duration'] / 60);//转换为分钟
        return $arr;
    }

    /**
     * 计算签名，平台订单验证签名
     *
     * @return string
     */
    protected function sign($rows, $sign, $secret=GAME_SECRET)
    {
        return parent::sign($rows, $sign, $secret);
    }

}
